import cryptoJs from 'crypto-js'
const key = cryptoJs.enc.Utf8.parse('WaveletAIKeyWord')
const iv = cryptoJs.enc.Utf8.parse('1234567887654321')
// import EncryptionDecryption from '@/utils/EncryptionDecryption.js'
// 加密
// let encryption = EncryptionDecryption.AES_Encrypt('111')
// 解密
// EncryptionDecryption.AES_Decrypt(encryption)
export default {
  // 加密函數
  AES_Encrypt(word) {
    let data = ''
    if (typeof word === 'string') {
      data = word
    } else if (typeof word === 'object') {
      data = JSON.stringify(word)
    }
    const srcs = cryptoJs.enc.Utf8.parse(data)
    const encrypted = cryptoJs.AES.encrypt(srcs, key, {
      // iv: iv,
      // mode: cryptoJs.mode.CBC,
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    })
    const hexStr = encrypted.ciphertext.toString().toUpperCase()
    const oldHexStr = cryptoJs.enc.Hex.parse(hexStr)
    // 将密文转为Base64的字符串
    const base64Str = cryptoJs.enc.Base64.stringify(oldHexStr)
    // 这里根据需求返回 base64Str 或 hexStr(解密时有小小差别)
    return base64Str
  },
  AES_Decrypt(word) {
    // 如果加密返回的base64Str
    const srcs = word
    // 若上面加密返回的hexStr,需打开下面两行注释，再次处理
    // let encryptedHexStr = cryptoJs.enc.Hex.parse(word);
    // let srcs = cryptoJs.enc.Base64.stringify(encryptedHexStr);
    const decrypt = cryptoJs.AES.decrypt(srcs, key, {
      iv: iv,
      // mode: cryptoJs.mode.CBC,
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    })
    const decryptedStr = decrypt.toString(cryptoJs.enc.Utf8)
    const value = decryptedStr.toString()
    return value
  },

  // 加密方法aes
  Encrypt(word, key = 'share') {
    let data = ''
    if (typeof word === 'string') {
      data = word
    } else if (typeof word === 'object') {
      data = JSON.stringify(word)
    }
    const encJson = cryptoJs.AES.encrypt(data, key).toString()
    const encData = cryptoJs.enc.Base64.stringify(cryptoJs.enc.Utf8.parse(encJson))
    return encData
  },
  // 解密方法aes
  Decrypt(word, key = 'share') {
    const decData = cryptoJs.enc.Base64.parse(word).toString(cryptoJs.enc.Utf8)
    const bytes = cryptoJs.AES.decrypt(decData, key).toString(cryptoJs.enc.Utf8)
    return JSON.parse(bytes)
  }
}
